<template>
	<view v-if="pageShow">
		<u-swiper :list="list1" height="200" circular radius="0"></u-swiper>
		<view class="mainpadding">
			<!-- 为成为会员的展示 -->
			<image :src="topbanner" mode="widthFix" @click="tzcwhy" v-if="userinfo.is_vip==0" style="width: 100%;"></image>
			<!-- 成为会员的展示 -->
			<view class="" v-if="userinfo.is_vip">
				<view class="sanshier xiaocheng textcenter">采购员身份识别码</view>
				<!-- <view class="erwem">
					<image src="../static/logo.png" mode=""></image>
				</view> -->
				<view class="flexcenter margin_top" v-if="!gq_status">
					<uqrcode ref="uqrcode" canvas-id="qrcode" @click="ewmfn" v-bind:value="qrcodeurl"
						:options="{ margin: 10}" size="300">
					</uqrcode>
				</view>
				<view class="flexcenter margin_top"  v-if="gq_status">
					<view class="ffffff flexcolumn radius" style="width: 300px;height: 300px;background-color: rgba(0, 0, 0, .01);" @click="getcaigouma()">
						<u-icon name="reload" color="#999" size="30"></u-icon>
						<view class="xiaohui titletext margin_top4">二维码已过期点击刷新</view>
					</view>
				</view>
				<!-- <view class="ershiba xiaohei margin_top textcenter">{{second=="二维码已过期点击刷新"?second:second+"秒"}}</view> -->
				<!-- <view class="ershiba xiaohei margin_top textcenter">{{second=="二维码已过期点击刷新"?second:second+"秒"}}</view> -->
				<view class="bigtext xiaohong margin_top1 textcenter" style="color: #ff0000;">二维码每10秒刷新，截图无效</view>
			</view>
			<view class="flexbetween margin_top6">
				<view class="xihbtn" @click="yibaoma()">医保码</view>
				<!-- <view class="xihbtn" @click="tzzfy">储值支付</view> -->
				<view class="xihbtn" @click="wxzfm()">微信支付码</view>
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				pageShow:true,
				topbanner:"",
				list1: [],
				bannerList: [],
				qrcodeurl:"",
				userinfo:"",
				second:10,
				gq_status:false,
				timer:null,
				cardExt:{}
			}
		},
		onLoad() {
			this.getbanner()
			this.getcardcode()
		},
		onShow() {
			this.getinfo()
		},
		onHide() {
			clearInterval(this.timer)
		},
		methods: {
			getcardcode(){
				httpRequest.request('/api/cardcl/cardSignature', 'GET', {
					card_id: 'pZN1464IEteJvuKUwyEWPtHp8oEM',
				}).then(res => {
					this.cardExt = res.data
				})
			},
			wxzfm(){
				if(this.userinfo.card_usercode!=0){
					this.openCard(this.userinfo.card_usercode)
				}else{
					this.addCard()
				}
			},
			addCard(){
				let that = this
				wx.addCard({
				  cardList: [
				    {
				      cardId: 'pZN1464IEteJvuKUwyEWPtHp8oEM',
				      cardExt:JSON.stringify(this.cardExt)
				    }
				  ],
				  success (res) {
				    console.log(res.cardList) // 卡券添加结果
					that.getEncryptCode(res.cardList[0].code)
				  }
				})
			},
			getEncryptCode(code){
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				httpRequest.request('/api/cardcl/getEncryptCode', 'GET', {
					encrypt_code:code
				}).then(res => {
					uni.hideLoading()
					this.openCard(res.data)
				})
			},
			openCard(data){
				wx.openCard({
				  cardList: [{
				    cardId: 'pZN1464IEteJvuKUwyEWPtHp8oEM',
				    code: data
				  }],
				  success (res) {
					  console.log(res,"888");
				  },fail(err) {
				  	console.log(err,"打开卡券失败");
				  }
				})
			},
			yibaoma(){
				uni.navigateToMiniProgram({
					// appid  写你要跳转的小程序的 appid
					appId: "wx7ec43a6a6c80544d",
					// 路径写  src下的路径,假如你跳转的是pages下的页面,就可以写pages/index
					path: "pages/new_index/index/index",
					extraData: {
						// 'type': 'out'
					},
					// 这个不写的话会显示开发环境,不能正常跳转,写上就能正常跳转了
					envVersion: 'release',
					success(res) {
						// 打开成功
						console.log("跳转成功");
						// uni.showToast({
						// 	title: '跳转成功'
						// })
					},
					fail(err) {
						// 打开失败/取消
						console.log("跳转不成功");
						// uni.showToast({
						// 	title: '跳转不成功'
						// })
					}
				})
			},
			ewmfn() {
				this.$refs.uqrcode.toTempFilePath({
					success: res => {
						console.log(res);
						var i = [res.tempFilePath]; //获取当前页面的轮播图数据
						//uniapp预览轮播图
						uni.previewImage({
							current: 0, //预览图片的下标
							urls: i //预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
						})
					}
				});
			},
			getbanner() {
				httpRequest.request('/api/index/getAdvertiseList', 'POST', {
					is_more:1,
					advertise_position_id:4
				}).then(res => {
					res.data.forEach(item => {
						this.list1.push(item.image_text)
					})
					this.bannerList = res.data
				})
				httpRequest.request('/api/index/getAdvertiseList', 'POST', {
					is_more:2,
					advertise_position_id:2
				}).then(res => {
					this.topbanner = res.data.image_text
					this.pageShow = true
				})
			},
			getinfo() {
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.userinfo = res.data
					if(res.data.is_vip){
						this.getcaigouma()
						
					}
				})
			},
			async getcaigouma(){
				let res = await httpRequest.request('/api/user/getVipCardCode', 'GET', {})
				if(res.code==1){
					this.gq_status = false
					this.qrcodeurl = res.data.code
					this.daojishi()
				}
			},
			daojishi(){
				this.timer = setInterval(()=>{
					if(this.second > 1){
						this.second--;
						console.log("过期状态:",this.gq_status);
					}else{
						clearInterval(this.timer);
						this.second = 10
						this.gq_status = true
					}
				},1000)
			},
			// 跳转购买会员
			tzcwhy() {
				uni.navigateTo({
					url: '/pages_mine/chengweihy'
				})
			},
			// 跳转支付页面
			tzzfy() {
				uni.navigateTo({
					url: '/pages_mine/chuzhizf'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.beijint1 {
		background-image: url('@/static/image/system/sy1.png');
	}

	.beijint2 {
		background-image: url('@/static/image/system/sy2.png');
	}

	.beijint3 {
		background-image: url('@/static/image/system/sy3.png');
	}

	.erwem {
		margin: 20rpx auto 0;
		width: 320rpx;
		height: 320rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.xihbtn {
		width: 300rpx;
		height: 78rpx;
		border-radius: 39rpx 39rpx 39rpx 39rpx;
		font-size: 32rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-weight: 500;
		color: #EE6D2D;
		border: 1rpx solid #EE6D2D;
	}

	.cwhybtn {
		margin: 30rpx auto 0;
		background: linear-gradient(105deg, #F15225 0%, #F6610D 33%, #FF9654 63%, #FC7D0B 100%);
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		font-size: 32rpx;
		font-weight: 500;
		color: #FFFFFF;
		width: 565rpx;
		height: 70rpx;
	}

	.webox {
		// background-color: #ef5212;
		background-size: 100% 100%;
		padding: 10rpx 36rpx 42rpx 10rpx;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
	}

	.vipcard {
		background-image: url("@/static/image/system/vipcard.png");
		width: 100%;
		// height: 345rpx;
		background-size: 100% 100%;
		position: relative;
		box-sizing: border-box;
	}
</style>